import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:village_android/config/contants.dart';
import 'package:village_android/main.dart';
import 'package:village_android/model/user_model.dart';
import 'package:village_android/pages/auth/register_first_page.dart';
import 'package:village_android/pages/auth/register_page.dart';
import 'package:village_android/utils/common_utils.dart';
import 'package:village_android/utils/http_utlis.dart';
import 'package:village_android/utils/store_utils.dart';
import 'dart:convert' as convert;

class ProfileVerifyDetailPage extends StatefulWidget {


  @override
  _ProfileVerifyDetailPageState createState() =>
      new _ProfileVerifyDetailPageState();
}

class _ProfileVerifyDetailPageState extends State<ProfileVerifyDetailPage> {
  final _formKey = GlobalKey<FormState>();

  //获取本地用户
  UserModel userModel = CommonUtils.getLocalUserInfo();


  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('党员认证'),
        ),
        body: Container(
          padding: EdgeInsets.all(20),
          child: Form(
            key: _formKey,
            child: ListView(
              children: <Widget>[
                SizedBox(height: 10.0),
                initPartyBranch(),
                SizedBox(height: 10.0),
                initPartyIdentity(),
                SizedBox(height: 10.0),
                initPartyJob(),
                SizedBox(height: 50.0),
                initButton(),
                SizedBox(height: 100.0),
              ],
            ),
          ),
        ));
  }


  TextFormField initPartyBranch() {
    return TextFormField(
      initialValue: userModel.partyBranch,
      onSaved: (String value) => userModel.partyBranch = value,
//      maxLength: 11,
      decoration: InputDecoration(labelText: '所在支部'),
    );
  }

  TextFormField initPartyIdentity() {
    return TextFormField(
      initialValue: userModel.partyIdentity,
      onSaved: (String value) => userModel.partyIdentity = value,
//      maxLength: 11,
      decoration: InputDecoration(labelText: '党员身份'),
    );
  }

  TextFormField initPartyJob() {
    return TextFormField(
      initialValue: userModel.partyJob,
      onSaved: (String value) => userModel.partyJob = value,
//      maxLength: 11,
      decoration: InputDecoration(labelText: '党内职务'),
    );
  }




  Align initButton() {
    return Align(
      child: SizedBox(
        width: 300.0,
        height: 50.0,
        child: RaisedButton(
          child: Text(
            '确定',
            style: TextStyle(color: Colors.white),
          ),
          color: Colors.red,
          onPressed: () {
            //CommonUtils.showToast(context, '登录');
            if (_formKey.currentState.validate()) {
              _formKey.currentState.save();
            }
            toDone();
          },
          shape: StadiumBorder(side: BorderSide(color: Colors.red)),
        ),
      ),
    );
  }

  Future toDone() async {
    FormData formData = new FormData.from(userModel.toJson());

    HttpUtils.postForm("/mgr/edit", formData: formData, success: (data) {
      CommonUtils.showToast(context, "修改成功");
      String jsonString = convert.jsonEncode(userModel);
      SharedPreferenceUtil.save(SharedPreferenceUtil.KEY_USERMODEL, jsonString);
      new Future.delayed(
          const Duration(seconds: 1),
              () =>
          {
            Navigator.of(context).pop()
          });
    }, error: (errorMsg) {
      CommonUtils.showToast(context, errorMsg);
    });
  }

}
